/**
 * Copyright © 2015 eqxiu.com 北京中网易企秀科技有限公司  All rights reserved.
 */
package com.eqxiu.ieditor.api.impl;

import com.eqxiu.ieditor.api.IPageTagServiceApi;
import com.eqxiu.ieditor.dao.PageTagMapper;
import com.eqxiu.ieditor.dto.PageTagDTO;
import com.eqxiu.ieditor.entity.PageTag;
import com.eqxiu.ieditor.entity.PageTagExample;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @description //TODO 设计说明
 * @author ranger
 * @date Jun 1, 2017
 */
@Service
@Transactional
public class PageTagServiceApiApiImpl implements IPageTagServiceApi {

	@Autowired
	private PageTagMapper pageTagMapper;

	@Override
	public int deleteByPrimaryKey(Long sceneId) {
		return pageTagMapper.deleteByPrimaryKey(sceneId);
	}
	@Override
	public int insert(PageTagDTO record) {
		PageTag tag = new PageTag();
		BeanUtils.copyProperties(record, tag);
		return pageTagMapper.insert(tag);
	}

	@Override
	public int insertSelective(PageTagDTO record) {
		PageTag tag = new PageTag();
		BeanUtils.copyProperties(record, tag);
		return pageTagMapper.insertSelective(tag);
	}

	@Override
	public List<PageTagDTO> selectBySceneId(Long sceneId) {
		PageTagExample example = new PageTagExample();
		example.setOrderByClause("page_id asc");
		example.createCriteria().andSceneIdEqualTo(sceneId);
		return getPageTagDTOS(example);
	}

	private List<PageTagDTO> getPageTagDTOS(PageTagExample example) {
		List<PageTag> tags = pageTagMapper.selectByExample(example);
		List<PageTagDTO> dtos= Lists.newArrayList();
		if (Objects.nonNull(tags)) {
		    for (PageTag tag:tags){
                PageTagDTO dto = new PageTagDTO();
                BeanUtils.copyProperties(tag, dto);
                dtos.add(dto);
            }
		}
		return dtos;
	}

	@Override
	public List<PageTagDTO> selectByPageId( Long pageId) {
		PageTagExample example = new PageTagExample();
		example.createCriteria().andPageIdEqualTo(pageId);
		return getPageTagDTOS(example);
	}

	@Override
	public List<PageTagDTO> selectByTagId( Integer tagId) {
		PageTagExample example = new PageTagExample();
		example.createCriteria().andTagIdEqualTo(tagId);
		return getPageTagDTOS(example);
	}

	@Override
	public PageTagDTO selectByPrimaryKey(Long pageId) {
		PageTag one = pageTagMapper.selectByPrimaryKey(pageId);
		PageTagDTO dto = new PageTagDTO();
		BeanUtils.copyProperties(one, dto);
		return dto;
	}

}
